/*
 * @Author: xuesong
 * @Date: 2022-06-07 16:15:35
 * @LastEditors: xuesong
 * @LastEditTime: 2022-06-08 20:21:00
 * @FilePath: \BLE_UART_EF\projects\ble_peripheral\uart_ble\userReadFlash\readFlash.c
 */

#include "flash.h"
#include "log.h"
#include "OSAL.h"
#include "readFlash.h"
#include "stdio.h"
#include "userApp.h"

static ReadFlash_infor_t readFlashInfor;
static uint8_t readFlashFlag = 0;
// static EF_test_str easyFlashTestData;
void SetReadFlashParament(uint8 *data,uint8 len)
{
    if(len >= 6)
    {
        readFlashInfor.Addr = *data++;
        readFlashInfor.Addr |= (*data++)<<8;
        readFlashInfor.Addr |= (*data++)<<16;
        readFlashInfor.Addr |= (*data++)<<24;

        readFlashInfor.len = *data++;
        readFlashInfor.len |= (*data)<<8;
        readFlashFlag &= (~B_READ_FLASH_FLAG_Init);
        LOG("read addr = %X ,Len = %X\r\n",readFlashInfor.Addr,readFlashInfor.len);
        useApp_task_set(USER_TASK_START,UserAppEvent_ReadFlash,32);
    }
}

uint16 ReadFlashAndPrint(void)
{
    static ReadFlash_infor_t STreadFlashInfor;
    uint8_t readBuff[16];
    uint32_t ReadAddr = 0;
    uint16_t ReadLen  = 0;
    if(!(readFlashFlag & B_READ_FLASH_FLAG_Init))
    {
        readFlashFlag |= B_READ_FLASH_FLAG_Init;
        STreadFlashInfor.Addr = readFlashInfor.Addr;
        STreadFlashInfor.len = readFlashInfor.len;
        LOG("addr\t");
        for(uint8_t i = 0 ; i < 16 ; i++)
        LOG("%X\t",i);
        LOG("\r\n");
    }
    ReadAddr = STreadFlashInfor.Addr;
    if(STreadFlashInfor.len < 16)
    {
        ReadLen = STreadFlashInfor.len;
    }else{
        ReadLen = 16;
    }
    STreadFlashInfor.len -= ReadLen;
    STreadFlashInfor.Addr += ReadLen;
    osal_memset(readBuff,0,sizeof(readBuff));
	HalFlashRead(ReadAddr, readBuff, ReadLen);

    LOG("0x%X\t",ReadAddr);
    for(uint16 i = 0; i < ReadLen ; i++)
    {
        LOG("%02X\t",readBuff[i]);
    }
        LOG("\r\n");
    return STreadFlashInfor.len;
}
